package io.bitmax.exchange.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import io.bitmax.exchange.R;

/**
 * Created by gongdongyang on 2018/9/28.
 * 自定义工具类
 */

public class TopBar extends RelativeLayout {

    private TextView tvTitle;

    private View left;

    private ImageView ivRight;

    private View right;

    private TextView tvRight;
    //数据
    private String title;

    private boolean leftVisibility;

    private Drawable rightIcon;

    private String rightText;

    public TopBar(Context context) {
        super(context);
        init(context, null);
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.app_topbar, this, true);

        tvTitle =  findViewById(R.id.topbar_title);
        left = findViewById(R.id.topbar_left);

        ivRight =  findViewById(R.id.topbar_right_img);

        right = findViewById(R.id.topbar_right);

        tvRight = findViewById(R.id.topbar_right_text);

        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TopBar);
            title = ta.getString(R.styleable.TopBar_bar_title);

            leftVisibility = ta.getBoolean(R.styleable.TopBar_bar_left_visibility, true);

            rightIcon = ta.getDrawable(R.styleable.TopBar_bar_right_icon);

            rightText = ta.getString(R.styleable.TopBar_bar_right_text);

            ta.recycle();
        }

        setTitle(title);

        left.setVisibility(leftVisibility ? VISIBLE : GONE);

        //默认是返回键效果
        left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getContext() instanceof Activity) {
                    ((Activity) getContext()).onBackPressed();
                }
            }
        });

        if (rightIcon != null) {
            ivRight.setVisibility(VISIBLE);
            ivRight.setImageDrawable(rightIcon);
        } else {
            ivRight.setVisibility(GONE);
        }

        if (TextUtils.isEmpty(rightText)) {
            tvRight.setVisibility(View.GONE);
        } else {
            tvRight.setVisibility(VISIBLE);
            tvRight.setText(rightText);
        }
    }

    public void setTitle(CharSequence title) {
        if (!TextUtils.isEmpty(title))
            tvTitle.setText(title);
    }

    public String getTitle(){
        return tvTitle.getText().toString();
    }

    public void setLeftClickListener(OnClickListener listener) {
        left.setOnClickListener(listener);
    }

    public void setRightClickListener(OnClickListener listener) {
        right.setOnClickListener(listener);
    }


    public void setRightText(CharSequence text){
        if (!TextUtils.isEmpty(text)){
            tvRight.setVisibility(VISIBLE);
            tvRight.setText(text);
        }else {
            tvRight.setVisibility(GONE);
        }
    }

    public View getRightView() {
        return right;
    }

    public View getRightTextView(){
        return tvRight;
    }

}
